package com.totoro.service.impl;

import com.totoro.mapper.OrderMapper;
import com.totoro.mapper.ShopMapper;
import com.totoro.pojo.*;
import com.totoro.service.OrderService;
import com.totoro.utils.Odd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author TOTORO
 * @date 2021/7/19 10:04
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    ShopMapper shopMapper;

    @Override
    public MarketOrder generateOrdersPojo(int userId, int sid, int arid, int orderStatus) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setMarketOdd(Odd.getOrderId(userId+""));
        marketOrder.setUid(userId);
        marketOrder.setOrderStatus(orderStatus);
        marketOrder.setSid(sid);
        marketOrder.setSlid(shopMapper.findShopBySid(sid).getSlid());
        marketOrder.setArid(arid);
        return marketOrder;
    }

    @Override
    public Integer addMarketOrder(MarketOrder marketOrder) {
        return orderMapper.addMarketOrder(marketOrder);
    }

    @Override
    public Integer addMarketSrOrder(MarketSrOrder marketSrOrder) {
        return orderMapper.addMarketSrOrder(marketSrOrder);
    }

    @Override
    public Integer updateMarketOrderPrevStatus(int status, String marketOdd) {
        return orderMapper.updateMarketOrderPrevStatus(status, marketOdd);
    }

    @Override
    public Map<String, OrderTimeline> findOrderTimeLineByMid(int mid) {
        List<OrderTimeline> timelines = orderMapper.findOrderTimeLineByMid(mid);
        Map<String, OrderTimeline> map = new HashMap<>();
        timelines.forEach(v ->{
            map.put(v.getOrderStatus()+"", v);
        });
        return map;
    }

    @Override
    public Integer addOrderTimeLineByMid(OrderTimeline orderTimeline) {
        return orderMapper.addOrderTimeLineByMid(orderTimeline);
    }

    @Override
    public MarketOrder findMarketOrderByMarketOdd(String marketOdd) {
        return orderMapper.findMarketOrderByMarketOdd(marketOdd);
    }

    @Override
    public List<MarketOrder> findAllOrder(Object id, Integer findType, Integer status) {
        return orderMapper.findAllOrder(id, findType, status);
    }

    @Override
    public List<MarketOrder> superFindUserOrderId(BuyerOrderFind buyerOrderFind) {
        Integer[] integers = orderMapper.superFindUserOrderId(buyerOrderFind);
        // 根据这些id查询分页的
        List<MarketOrder> orderList = new ArrayList<>();
        if(integers.length > 0){
            orderList = orderMapper.finalFindAllOrderByMoIds(integers);
        }
        return orderList;
    }

    @Override
    public Integer falseDeleteOrder(int moId) {
        return orderMapper.falseDeleteOrder(moId);
    }

    @Override
    public List<MarketOrder> findRecoverOrder(int uid, int curr) {
        Integer[] integers = orderMapper.findRecoverOrder(uid, curr);
        // 根据这些id查询分页的
        List<MarketOrder> orderList = new ArrayList<>();
        if(integers.length > 0){
            orderList = orderMapper.finalFindAllOrderByMoIds(integers);
        }
        return orderList;
    }

    @Override
    public Integer[] notPaginationRecoverOrder(int uid, int curr) {
        return orderMapper.findRecoverOrder(uid, curr);
    }

    @Override
    public Integer trueDeleteOrder(int[] moIds) {
        return orderMapper.trueDeleteOrder(moIds);
    }

    @Override
    public Integer reductionOrder(int[] moIds) {
        return orderMapper.reductionOrder(moIds);
    }

    @Override
    public List<MarketOrder> superFindSellerOrderId(SellerOrderFind sellerOrderFind) {
        Integer[] integers = orderMapper.superFindSellerOrderId(sellerOrderFind);
        List<MarketOrder> orderList = new ArrayList<>();
        if(integers.length > 0){
            orderList = orderMapper.finalFindAllOrderByMoIds(integers);
        }
        return orderList;
    }

    @Override
    public Integer updateOrder(MarketOrder marketOrder) {
        return orderMapper.updateOrder(marketOrder);
    }

    @Override
    public Integer updateSrOrderPayPrice(int msroId, double payPrice) {
        return orderMapper.updateSrOrderPayPrice(msroId, payPrice);
    }

    @Override
    public Integer updateOrderAllPrice(int moId, double allPrice) {
        return orderMapper.updateOrderAllPrice(moId, allPrice);
    }

    @Override
    public Integer updateMarketSrOrderCommentStatus(int msroId) {
        return orderMapper.updateMarketSrOrderCommentStatus(msroId);
    }

    @Override
    public Integer updateMarketSrOrderAppendCommentStatus(int msroId) {
        return orderMapper.updateMarketSrOrderAppendCommentStatus(msroId);
    }

    @Override
    public MarketOrder findMarketByMarketSrOrderId(int msroId) {
        return orderMapper.findMarketByMarketSrOrderId(msroId);
    }

    @Override
    public MarketSrOrder findMarketSrOrderByMsroId(int msroId) {
        return orderMapper.findMarketSrOrderByMsroId(msroId);
    }

    @Override
    public Integer updateMarketSrReturnStatus(int status, int status2, int msroId) {
        return orderMapper.updateMarketSrReturnStatus(status, status2, msroId);
    }
}
